<?php
defined('BASEPATH') OR exit('No direct script access allowed');

//用户
class User extends MY_Controller {

	function __construct(){
		parent::__construct();
		
	}	
	
	function VerifToken(){
		$farr = array("login","send_code",'check_login');
		$method = $this->router->fetch_method(); 
		if(in_array($method,$farr)){
			return true;
		}
		parent::VerifToken();
	}
	
	public function check_login(){
		$code = $this->input->post('code',null,'');//小程序登录时获取的code
		if(empty($code)){
			$this->err_out('参数错误');
		}
		
		$this->load->model('User/UserLoginModel', 'UserLoginModel');
		$openid = $this->UserLoginModel->get_openid($code);
		
		$this->load->model('User/UserModel', 'UserModel');
		
		$info = $this->UserModel->_field_get_info($openid,'openid');
		if($info){
			$is_bind = 1;
			$token = $this->UserLoginModel->create_token($info['id']);
		}
		else{
			$is_bind = 0;
			$token = $openid;
		}
		
		
		$this->load->model('Admin/ConfigModel', 'configs');
		$disabled = $this->configs->unique_get_info("name","user_examine");
		
		$abled = 0;//是否开启新用户验证
		if($disabled['info']==1){
			$abled = 1;//需要验证
		}
		else{
			$abled = 0;//不需要验证
		}
		
		$data = array(
			"data"=>$token,									
			"disabled"=>$abled,
			"is_bind"=>$is_bind
		);
		
		$this->data_out($data);
	}
	//手机登录
	public function login(){
		$openid = $this->input->post('openid',null,'');//小程序登录时获取的code
		$mobile = $this->input->post('mobile',null,'');//小程序登录时要绑定的手机号码
		$code = $this->input->post('code',null,'');//小程序登录时绑定的手机验证码
		
		$ver_arr = array(
			'code'=>'验证码不能为空',
			'openid'=>'OPENID不能为空',
			'mobile'=>array(
				'required'=>"手机号码不能为空",
				'exact_length[11]'=>'请输入正确的手机号码',
				'regex_match[/^1[34578]\d{9}$/]'=>'请输入正确的手机号码',
			)
		);
		
		$result = $this->CheckData($ver_arr,array('mobile'=>$mobile,'code'=>$code,'openid'=>$openid));
		if($result){
			$this->err_out($result);
		}
		
		$this->load->model('User/ValidateRecordModel','validate');		
		
		if($this->validate->validate_code($mobile,$code,'login')!==true){
			$this->err_out('验证码错误');
		}
		
		
		$time = time();		
		$this->load->model('User/UserLoginModel', 'login');
		$res = $this->login->login($openid,$mobile);//自动注册		
		if(!$res){
			$this->err_out($this->login->getErr());
		}
		
		
		$token = $this->login->create_token($res['user_id']);
		
		$data = array(
			"token"=>$token,									
			"disabled"=>$res['disabled']
		);
		
		$this->data_out($data);
	}
	
	//发送验证码
	function send_code(){
		$mobile = $this->input->post('mobile');		
		
		if(empty($mobile)){
			$this->err_out('手机号码不能为空');
		}
		
		$this->load->model('User/ValidateRecordModel','validate');		

		$code = rand(100000,999999);
		$this->validate->save_validate_record($mobile,$code,'login');

		
		$data = array('data'=>1,'message'=>'发送手机号码成功');
		$this->out($data);
	}
	
	//绑定手机号
	function auth_mobile(){
		$user_id 	= $this->user_id;
		$mobile = $this->input->post('mobile');
		$code   = $this->input->post('code');
		
		if(empty($mobile) or empty($code)){
			$this->err_out('手机和验证码不能为空');
		}
		
		$this->load->model('User/UserModel', 'user');
		$info = $this->user->get_info($user_id);
		if(!$info){
			$this->err_out('帐号不存在');
		}	
		
		$unique = $this->user->unique_get_info("mobile",$mobile);
		if($unique){
			$this->err_out('该号码已被使用');
		}
		
		$this->load->model('User/ValidateRecordModel', 'vcode');
		$record = $this->vcode->mobile_record($mobile,$code);
		if(empty($record)){
			$this->err_out('验证码错误');
		}
		$this->vcode->remove_validate_record($mobile,'');
		
		
		$res = $this->user->edit(array("mobile"=>$mobile),"user_id='{$user_id}'");
		
		$data['result_code'] = $res?0:1;
		$data['message'] = $res?'绑定成功':'绑定失败';
		$this->out($data);
	}
	
	//会员授权获取信息
	public function binding(){
		$user_id 	= $this->user_id;
		$nickname	= $this->input->post('nickName',null,'');	//昵称
		$headimg	= $this->input->post('avatarUrl',null,'');	//头像		
		$sex		= $this->input->post('gender',null,'');		//性别
		$city		= $this->input->post('city',null,'');		//城市
		$province	= $this->input->post('province',null,'');	//省份
		$country	= $this->input->post('country',null,'');	//国家
		
		$time = time();
		
		if(!$nickname){
			$this->err_out('昵称不能为空');
		}
		if(!$headimg){
			$this->err_out('头像不能为空');
		}
		
		$this->load->model('User/UserModel', 'user');
		$info = $this->user->get_info($user_id);
		if(!$info){
			$this->err_out('帐号不存在');
		}
		
		$arr = array(
			'headimg' 	=> $headimg,			
			'nickname' 	=> $nickname,
			'sex' 		=> $sex,
			'city' 		=> $city,
			'province' 	=> $province,
			'country' 	=> $country,			
			'addtime' 	=> $time
		);
		
		$res = $this->user->edit($arr,"id='{$user_id}'");
		
		$data['result_code'] = $res?0:1;
		$data['message']     = $res?'授权成功':'授权失败';
		$data['data'] 		 = '';
		$this->out($data);
	}
	
	//获取用户信息
	function info(){
		$user_id = $this->user_id;
		$this->load->model('User/UserModel','user');
		
		$user = $this->user->get_info($user_id);
		if(!$user){
			$this->err_out("用户不存在");
		}
		
		$user['addtime'] = date('Y-m-d H:i:s',$user['addtime']);
		
		$data = array('data'=>$user,'message'=>'获取数据成功');
		$this->out($data);
	}
	
	//更改用户信息
	function updata_user(){
		$user_id = $this->user_id;		
		$nickname	= $this->input->post('nickName',null,'');	//昵称
		$headimg	= $this->input->post('avatarUrl',null,'');	//头像		
		$sex		= $this->input->post('gender',null,'');		//性别
		$city		= $this->input->post('city',null,'');		//城市
		$info		= $this->input->post('info',null,'');		//介绍
		$birthday	= $this->input->post('birthday',null,'');	//介绍
		$mobile		= $this->input->post('mobile',null,'');		//联系方式
		
		$this->load->model('User/UserModel','user');
		
		$user = $this->user->get_info($user_id);
		if(!$user){
			$this->err_out("用户不存在");
		}		
		if(!$nickname){
			$this->err_out('昵称不能为空');
		}
		if(!$headimg){
			$this->err_out('头像不能为空');
		}
		
		$arr = array(
			"headimg"  => $headimg,
			"nickname" => $nickname,			
			"sex" 	   => $sex,
			"city" 	   => $city,
			"info"     => $info,
			"birthday" => $birthday,
			"mobile"   => $mobile
		);
		
		
		$res = $this->user->edit($arr," id='{$user_id}' ");
		
		$data['result_code'] = $res?0:1;
		$data['message']     = $res?'更新成功':'更新失败';
		$data['data'] 		 = $res;	
		$this->out($data);
	}
	
	
}
